package com.neusoft.yuejian26.yuejian_26.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.neusoft.yuejian26.yuejian_26.Adapter.ActAdapter;
import com.neusoft.yuejian26.yuejian_26.Adapter.MatchAdapter;
import com.neusoft.yuejian26.yuejian_26.Adapter.ProAdapter;
import com.neusoft.yuejian26.yuejian_26.Bean.Actbean;
import com.neusoft.yuejian26.yuejian_26.Bean.Matchbean;
import com.neusoft.yuejian26.yuejian_26.Bean.Probean;
import com.neusoft.yuejian26.yuejian_26.R;
import com.neusoft.yuejian26.yuejian_26.model.ActivityModel;
import com.neusoft.yuejian26.yuejian_26.model.MatchModel;
import com.neusoft.yuejian26.yuejian_26.model.ProModel;
import com.neusoft.yuejian26.yuejian_26.network.RetrofitCallback;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    public TextView home_act_btn, home_pro_btn, home_match_btn;
    public RelativeLayout home_act,home_pro,home_match;
    public ImageView act_line,pro_line,match_line;
    public View view = null;
    public XRecyclerView home_list;
    private RollPagerView rollPVhome = null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        initview();
        initClick();
        initEvents();
        getData();
        return view;
    }

    public void getData() {
        ActivityModel activityModel=new ActivityModel();
        Call call = activityModel.actList("1");
        activityModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                List<Actbean> mlist = (List<Actbean>) o;
                home_list.setAdapter(new ActAdapter(getActivity(), mlist));
            }
        });
    }

    public void getProData(){
        ProModel proModel = new ProModel();
        Call call = proModel.proList("1");
        proModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                List<Probean> mplist = (List<Probean>) o;
                home_list.setAdapter(new ProAdapter(getActivity(),mplist));
            }
        });
    }

    public void getMatchData() {
        MatchModel matchModel=new MatchModel();
        Call call =matchModel.matchList("1");
        matchModel.bindCallback(call, new RetrofitCallback() {
            @Override
            public void onResponse(Object o) {
                List<Matchbean> mtlist = (List<Matchbean>) o;
                home_list.setAdapter(new MatchAdapter(getActivity(), mtlist));
            }
        });
    }

    //轮播图
    public void initEvents() {
        rollPVhome.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPVhome.setAnimationDurtion(500);
        //设置指示器：
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPVhome.setHintView(new ColorPointHintView(getActivity(),
                getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPVhome.setAdapter(new RollPagerAdapter());
        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/

    }

    private int images[] = {
            R.drawable.homeroll_1,
            R.drawable.homeroll_2,
            R.drawable.homeroll_3
    };

    //设置适配器
    private class RollPagerAdapter extends StaticPagerAdapter {

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }

    private void initview() {
        home_list = (XRecyclerView) view.findViewById(R.id.activity_list);
        home_list.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        rollPVhome = (RollPagerView) view.findViewById(R.id.rollPV_home);
        home_act_btn = (TextView) view.findViewById(R.id.home_act_btn);
        home_pro_btn = (TextView) view.findViewById(R.id.home_pro_btn);
        home_match_btn = (TextView) view.findViewById(R.id.home_match_btn);
        act_line= (ImageView) view.findViewById(R.id.act_line);
        pro_line= (ImageView) view.findViewById(R.id.pro_line);
        match_line= (ImageView) view.findViewById(R.id.match_line);
        home_act= (RelativeLayout) view.findViewById(R.id.home_act);
        home_pro= (RelativeLayout) view.findViewById(R.id.home_pro);
        home_match=(RelativeLayout) view.findViewById(R.id.home_match);
    }

    private void initClick(){
        home_act.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resetTextColor();
                home_act_btn.setTextColor(0XFF72BD9C);
                resetLineColor();
                act_line.setImageResource(R.drawable.line_checked);
                getData();
            }
        });
        home_pro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resetTextColor();
                home_pro_btn.setTextColor(0XFF72BD9C);
                resetLineColor();
                pro_line.setImageResource(R.drawable.line_checked);
                getProData();
            }
        });
        home_match.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTextColor();
                home_match_btn.setTextColor(0XFF72BD9C);
                resetLineColor();
                match_line.setImageResource(R.drawable.line_checked);
                getMatchData();
            }
        });
    }

    private void resetTextColor() {
        home_act_btn.setTextColor(0XFFACACAB);
        home_match_btn.setTextColor(0XFFACACAB);
        home_pro_btn.setTextColor(0XFFACACAB);
    }

   private  void resetLineColor(){
        act_line.setImageResource(R.drawable.line_unchecked);
        pro_line.setImageResource(R.drawable.line_unchecked);
        match_line.setImageResource(R.drawable.line_unchecked);
    }

}
